﻿namespace Hotel.Web.Controllers
{
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Hotel.Web.Models;

    public class RoomServiceController : Controller
    {
        //
        // GET: /RoomService/
        [HttpGet]
        [Authorize(Roles = "Repairman, Cleaners")]
        public ActionResult Index()
        {
            RoomCleaner model = GetAssignments();
            return View(model);
        }

        [HttpGet]
        [Authorize(Roles = "Repairman, Cleaners")]
        public ActionResult Current()
        {
            using (Hotel.Module.Management.ManagementController m_ManagementController = Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Management.IManagement)) as Hotel.Module.Management.ManagementController)
            {
                List<Hotel.Module.Management.Models.Assignment> assList = m_ManagementController.GetCurrentAssignments(User.Identity.Name);
                RoomCleaner model = RoomCleaner.FromDomainModel(assList);
                return View(model);
            }
        }

        [HttpPost]
        [Authorize(Roles = "Repairman, Cleaners")]
        public ActionResult Current(RoomCleaner model)
        {
            #region validation logic
            if (string.IsNullOrEmpty(model.SelectedRoom))
            {
                ModelState.AddModelError("", Hotel.Web.Views.Assignment.AssignmentResources.ChooseAssignmentError);
                return View(model);
            }
            #endregion
            try
            {
                using (Hotel.Module.Management.ManagementController m_ManagementController = Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Management.IManagement)) as Hotel.Module.Management.ManagementController)
                {
                    List<Hotel.Module.Management.Models.Assignment> assList = m_ManagementController.GetCurrentAssignments(User.Identity.Name);
                    RoomCleaner storageOfTasks = RoomCleaner.FromDomainModel(assList);

                    string[] arguments = model.SelectedRoom.Split(' ');
                    Apply apply = new Apply();
                    foreach (var item in storageOfTasks.Assignments)
                    {
                        if (item.RoomNumber.ToString() == arguments[1] && item.CurrentService.Name.Split(' ')[0] == arguments[2])
                        {
                            apply.assignment = item;
                            break;
                        }
                    }
                    return View("Apply", apply);
                }
            }
            catch
            {
                return View(model);
            }
        }


        [HttpPost]
        [Authorize(Roles = "Repairman, Cleaners")]
        public ActionResult Confirmation(RoomCleaner model)
        {
            try
            {
                RoomCleaner Assignments = GetAssignments();
                string[] arguments = model.SelectedRoom.Split(' ');
                Apply apply = new Apply();
                foreach (var item in Assignments.Assignments)
                {
                    if (item.RoomNumber.ToString() == arguments[1] && item.CurrentService.Name.Split(' ')[0] == arguments[2])
                    {
                        apply.assignment = item;
                        break;
                    }
                }

                return View("Confirmation", apply);
            }
            catch
            {
                return View("Index", GetAssignments());
            }
        }

        [HttpPost]
        [Authorize(Roles = "Repairman, Cleaners")]
        public ActionResult Apply(Apply model)
        {
            try
            {
                RoomCleaner storageOfTasks = GetAssignments();
                foreach (var item in storageOfTasks.Assignments)
                {
                    if (item.RoomNumber == model.assignment.RoomNumber && item.CurrentService.Name == model.assignment.CurrentService.Name)
                    {
                        model.assignment = item;
                        break;
                    }
                }
                model.assignment.WorkerLogin = User.Identity.Name;
                using (Hotel.Module.Management.ManagementController m_ManagementController = Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Management.IManagement)) as Hotel.Module.Management.ManagementController)
                {
                    Hotel.Module.Management.Models.Assignment ass = new Module.Management.Models.Assignment();
                    model.assignment.AssignTo(ass);
                    m_ManagementController.ApplyAssignment(ass);
                }
            }
            catch 
            {
                ModelState.AddModelError("", Hotel.Web.Views.Assignment.AssignmentResources.ErrorSummary);
            }
            return View(model);
        }

        [HttpPost]
        [Authorize(Roles = "Repairman, Cleaners")]
        public ActionResult WorkResult(Apply model)
        {
            try
            {
                using (Hotel.Module.Management.ManagementController m_ManagementController = Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Management.IManagement)) as Hotel.Module.Management.ManagementController)
                {
                    List<Hotel.Module.Management.Models.Assignment> assList = m_ManagementController.GetCurrentAssignments(User.Identity.Name);
                    RoomCleaner storageOfTasks = RoomCleaner.FromDomainModel(assList);

                    foreach (var item in storageOfTasks.Assignments)
                    {
                        if (item.RoomNumber == model.assignment.RoomNumber && item.CurrentService.Name == model.assignment.CurrentService.Name)
                        {
                            model.assignment = item;
                            break;
                        }
                    }
                }
                if (model.ActionType.ToString() == "Cancel")
                {
                    model.assignment.WorkerLogin = null;
                    using (Hotel.Module.Management.ManagementController m_ManagementController = Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Management.IManagement)) as Hotel.Module.Management.ManagementController)
                    {
                        Hotel.Module.Management.Models.Assignment ass = new Module.Management.Models.Assignment();
                        model.assignment.AssignTo(ass);
                        m_ManagementController.ApplyAssignment(ass);
                    }
                }
                if (model.ActionType.ToString() == "Finish")
                {
                    using (Hotel.Module.Management.ManagementController m_ManagementController = Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Management.IManagement)) as Hotel.Module.Management.ManagementController)
                    {
                        Hotel.Module.Management.Models.Assignment ass = new Module.Management.Models.Assignment();
                        model.assignment.AssignTo(ass);
                        m_ManagementController.FinishAssignment(ass);
                    }
                    return View("Success", model);
                }
            }
            catch
            {
                ModelState.AddModelError("", Hotel.Web.Views.Assignment.AssignmentResources.ChooseAssignmentError);
            }
            return View("Index", GetAssignments());
        }

        private RoomCleaner GetAssignments()
        {
            using (Hotel.Module.Management.ManagementController m_ManagementController = Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Management.IManagement)) as Hotel.Module.Management.ManagementController)
            {
                string role = (User.IsInRole("Cleaners") ? Assignment.CurrentWorkerType.Cleaners : Assignment.CurrentWorkerType.Repairman).ToString();
                List<Hotel.Module.Management.Models.Assignment> DBList = m_ManagementController.GetAssignmentsToDo(role);
                RoomCleaner model = RoomCleaner.FromDomainModel(DBList);
                return model;
            }
        }
    }
}
